package com.westcom.vulb.logic;

import java.io.File;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;

import org.apache.http.Header;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.message.BasicHeader;
import org.json.JSONException;
import org.json.JSONObject;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;

import com.j256.ormlite.dao.RuntimeExceptionDao;
import com.westcom.vulb.application.keyBean;
import com.westcom.vulb.bean.Session;
import com.westcom.vulb.network.http.VulbHttp;
import com.westcom.vulb.protocal.http.Response;

public class account extends BasicLogic {
	public static String HeartBeat_url = base_url+"web/heart_beat";
	public static String login_url = base_url+"web/login_normal";
	public static String logout_url =base_url+"web/logout";
	public static String token_login_url = base_url+"web/login_fast";
	public static String CheckMailAccountDuplication_url = base_url+"web/check_account_duplicate";
	public static String CreateAccount_url = base_url + "web/create_user";
	public static String UploadProfileImage_url = base_url + "web/upload_person_img";
	public static String GetProfileImage_url = base_url + "web/person_img";
	public static String GetProfile_url = base_url + "web/select_profile";
	public static String UpdateProfile_url = base_url + "web/update_profile";
	public static String PwdUpdate_url = base_url + "web/update_password";
	public static String PwdResetReq_url = base_url + "web/reset_password_request";
	public static String PwdReset_url = base_url + "web/";
	public static String GetUserConfig_url = base_url + "web/reference_config";
	public static String UpdateUserConfig_url = base_url + "web/update_config";
	public static String UpdateMail_url = base_url + "web/update_mail";
	public static account getInstanse()
	{
		return new account();
	}
    public Response login(String mAccount, String mPwd) {
        keyBean md5 = new keyBean();
        String passwordmd5 = md5.getkeyBeanofStr(mPwd);
        JSONObject j = new JSONObject();
        try 
        {
			j.put("mailAddress", mAccount);
			j.put("password", passwordmd5);
	        j.put("needToken", "0");
		} catch (JSONException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.addTextBody("params", j.toString());
        builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
        Response response = VulbHttp.post(login_url,buildHeader(null),builder.build());
        
        if (response.errno == Response.SUCCESS) 
        {
            SessionManager sm = SessionManager.getInstance();
            int uid = Integer.valueOf(response.data.get("userId").toString());
            String sessionid = (String) response.data.get("sessionId");
            Session s = new Session(uid,sessionid,Calendar.getInstance(),mAccount,mPwd);
            sm.login(s);
        }
        return response;
    }
    public Response logout(String sessionId)
    {
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.addTextBody("params", "");
        builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
    	Response response = VulbHttp.post(logout_url,buildHeader(sessionId),builder.build());
    	return response;
    }
    public Response TokenLogIn(String token)
    {
         JSONObject j = new JSONObject();
         try 
         {
 			j.put("tokenId", token);
 		} catch (JSONException e) 
 		{
 			// TODO Auto-generated catch block
 			e.printStackTrace();
 		}
         MultipartEntityBuilder builder = MultipartEntityBuilder.create();
         builder.addTextBody("params", j.toString());
         builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
    	 Response response = VulbHttp.post(login_url,null,builder.build());
    	 
    	 if (response.errno == Response.SUCCESS) 
         {
             SessionManager sm = SessionManager.getInstance();
             int uid = Integer.valueOf(response.data.get("userId").toString());
             String sessionid = (String) response.data.get("sessionId");
         }
         return response;
    }
    public Response CheckMailAccountDuplication(String mailAccount)
    {
    	JSONObject j = new JSONObject();
        try 
        {
			j.put("mailAddress", mailAccount);
		} catch (JSONException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.addTextBody("params", j.toString());
        builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
    	Response response = VulbHttp.post(CheckMailAccountDuplication_url,buildHeader(null),builder.build());
        return response;
    }
    public Response CreateAccount(String mailAddress,String pwd,String userName)
    {
    	keyBean md5 = new keyBean();	
    	JSONObject j = new JSONObject();
        try 
        {
			j.put("mailAddress", mailAddress);
			j.put("password", md5.getkeyBeanofStr(pwd));
	        j.put("userName", userName);
		} catch (JSONException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.addTextBody("params", j.toString());
        builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
    	Response response = VulbHttp.post(CreateAccount_url,buildHeader(null),builder.build());
    	return response;
    }
    public Response UploadProfileImage(String pic_path,String sessionId)
    {
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.addBinaryBody("file", new File(pic_path));
        builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
    	Response response = VulbHttp.post(UploadProfileImage_url,buildHeader(sessionId),builder.build());
    	return response;
    }
    public Response GetProfileImage(int userId,String pic_path,String sessionId)
    {
    	JSONObject j = new JSONObject();
        try 
        {
			j.put("userId", userId);
		} catch (JSONException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.addTextBody("params", j.toString());
        builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
    	Response response = VulbHttp.post(GetProfileImage_url,buildHeader(sessionId),builder.build());
    	return response;
    }
    public Response GetProfile(String sessionId,int userId)
    {
    	JSONObject j = new JSONObject();
        try 
        {
			j.put("userId", userId);
		} catch (JSONException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.addTextBody("params", j.toString());
        builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
    	Response response = VulbHttp.post(GetProfile_url,buildHeader(sessionId),builder.build());
    	return response;
    }
    public Response UpdateProfile(String sessionId,HashMap<String, String> profile)
    {
    	JSONObject j = profile2json(profile);
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.addTextBody("params", j.toString());
        builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
    	Response response = VulbHttp.post(UpdateProfile_url,buildHeader(sessionId),builder.build());
    	return response;
    }
    public Response PwdUpdate(String sessionId,String pwd,String newPwd)
    {
    	JSONObject j = new JSONObject();
        try 
        {
			j.put("password", pwd);
			j.put("newPassword", newPwd);
		} catch (JSONException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.addTextBody("params", j.toString());
        builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
    	Response response = VulbHttp.post(PwdUpdate_url,buildHeader(sessionId),builder.build());
    	return response;
    }
    public Response PwdResetReq(String mailAddress)
    {
    	JSONObject j = new JSONObject();
        try 
        {
			j.put("mailAddress", mailAddress);
		} catch (JSONException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.addTextBody("params", j.toString());
        builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
    	Response response = VulbHttp.post(PwdResetReq_url,buildHeader(null),builder.build());
    	return response;
    }
    public Response PwdReset(int userId,String confirmKey,String newPwd)
    {
    	JSONObject j = new JSONObject();
        try 
        {
			j.put("userId", userId);
			j.put("confirmKey", confirmKey);
			j.put("newPassword", newPwd);
		} catch (JSONException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.addTextBody("params", j.toString());
        builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
    	Response response = VulbHttp.post(PwdReset_url,buildHeader(null),builder.build());
    	return response;
    }
    public Response GetUserConfig(String sessionId)
    {
    	 MultipartEntityBuilder builder = MultipartEntityBuilder.create();
    	 builder.addTextBody("params", "");
         builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
         Response response = VulbHttp.post(GetUserConfig_url,buildHeader(sessionId),builder.build());
         return response;
    }
    public Response UpdateUserConfig(String sessionId,String local,String userConfig)
    {
    	JSONObject j = new JSONObject();
        try 
        {
			j.put("locale", local);
			j.put("userConfig", userConfig);
		} catch (JSONException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.addTextBody("params", j.toString());
        builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
    	Response response = VulbHttp.post(PwdReset_url,buildHeader(null),builder.build());
    	return response;
    }
    public Response HttpHeartBeat(String sessionId)
    {
    	MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
        Response response = VulbHttp.post(HeartBeat_url,buildHeader(sessionId),builder.build());
        return response;
    }
    public Response UpdateMail(String sessionId,String mail,String pwd)
    {
    	keyBean md5 = new keyBean();
        String pwdMD5 =  md5.getkeyBeanofStr(pwd);
        JSONObject j = new JSONObject();
        try 
        {
			j.put("mailAddress", mail);
			j.put("password", pwdMD5);
		} catch (JSONException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.addTextBody("params", j.toString());
        builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
    	Response response = VulbHttp.post(UpdateMail_url,buildHeader(null),builder.build());
        return response;
    }
    public static JSONObject profile2json(HashMap<String, String> profile)
    {
    	JSONObject j= new JSONObject();
    	Iterator it = profile.entrySet().iterator();
 
        while (it.hasNext()) 
        {  
            Entry e = (Entry) it.next();
            try 
            {
				j.put((String)e.getKey(), e.getValue());
			} catch (JSONException e1) 
			{
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
        }  
    	return j;
    }
    public static Bitmap Base64Str2Bitmap(String image_str)
    {
        byte[] img_bytes = Base64.decode(image_str, Base64.DEFAULT);
        Bitmap image = BitmapFactory.decodeByteArray(img_bytes, 0,img_bytes.length);
        return image;
    }
}